package com.order.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.order.api.dao.TblLogDao;
import com.order.api.po.TblLog;
import com.order.api.po.TblLogExample;
import com.order.api.service.LogService;
import com.order.api.service.UsersService;
import com.order.api.vo.request.LogListRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private TblLogDao tblLogDao;

    @Autowired
    private UsersService usersService;

    @Override
    public void save(TblLog tblLog) {
        tblLogDao.insertSelective(tblLog);
    }

    @Override
    public void update(TblLog tblLog) {
        tblLogDao.updateByPrimaryKeySelective(tblLog);
    }

    @Override
    public PageInfo<TblLog> LogList(LogListRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        TblLogExample example = new TblLogExample();
        example.setOrderByClause("id DESC");
        TblLogExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(request.getState())) {
            criteria.andStatusEqualTo(request.getState());
        }
        List<TblLog> list = tblLogDao.selectByExample(example);
        if (list.size() > 0) {
            Map<Long, String> map = usersService.getIdAndName(list.stream().map(TblLog::getCreateId).distinct().collect(Collectors.toList()));
            for (TblLog tblLog : list) {
                tblLog.setCreateName(map.getOrDefault(tblLog.getCreateId(), "-"));
            }
        }
        return new PageInfo<>(list);
    }
}
